import Vue from 'vue'
import VueRouter from 'vue-router'
import { Toast } from 'vant'
Vue.use(VueRouter)

const routes = [
  {
    path: '/',
    name: 'Layout',
    redirect: '/home',
    component: () => import(/* webpackChunkName: "Layout" */ '@/views/layout/layout.vue')
  },
  {
    path: '/home',
    name: 'Home',
    component: () => import(/* webpackChunkName: "Layout" */ '@/views/tabbar/HomeBar.vue'),
    meta: { showTabbar: true }
  },
  {
    path: '/home/popup',
    name: 'homePopup',
    component: () => import(/* webpackChunkName: "homePopup" */ '@/views/home/homePopup.vue'),
    meta: { toRight: true }
  },
  {
    path: '/category',
    name: 'Category',
    component: () => import(/* webpackChunkName: "Category" */ '@/views/tabbar/categoryBar.vue'),
    meta: { showTabbar: true }
  },
  {
    path: '/cart',
    name: 'Cart',
    component: () => import(/* webpackChunkName: "Cart" */ '@/views/tabbar/cartBar.vue'),
    meta: { showTabbar: true }
  },
  {
    path: '/topic',
    name: 'Topic',
    component: () => import(/* webpackChunkName: "Topic" */ '@/views/tabbar/topicBar.vue'),
    meta: { showTabbar: true }
  },
  {
    path: '/user',
    name: 'User',
    component: () => import(/* webpackChunkName: "User" */ '@/views/tabbar/userBar.vue'),
    meta: { showTabbar: true }
  },
  {
    path: '/channel/:id',
    name: 'Channel',
    props: true,
    component: () => import(/* webpackChunkName: "Channel" */ '@/views/home/Channel.vue')
  },
  {
    path: '/brand/:id',
    name: 'Brand',
    props: true,
    component: () => import(/* webpackChunkName: "Brand" */ '@/views/home/brandDetail.vue')
  },
  {
    path: '/details/:id',
    name: 'details',
    props: true,
    component: () => import(/* webpackChunkName: "Brand" */ '@/views/home/goodsDetalis.vue')
  }
]

const router = new VueRouter({
  routes
})
router.beforeEach((to, from, next) => {
  const token = localStorage.getItem('token')
  // 如果没有token
  if (!token && to.path === '/cart') {
    Toast('您暂未登录请先登录')
    setTimeout(() => {
      router.push('/user')
    }, 1000)
  } else {
    next()
  }
})
export default router
